<?php
require 'PHPUnit/Framework.php';
require 'SJK.php';
class SJK_Test extends PHPUnit_Framework_TestCase {
    public function testInsert() {
        $sjk=$this->getSJK();
        $sjk->initSql('drop table if exists t1');
        $this->assertEquals(true,$sjk->query());
        $sjk->initSql('create table t1(id int auto_increment,v char(4),PRIMARY KEY (id))TYPE=MyISAM;');
        $this->assertEquals(true,$sjk->query());

        echo 'testing reconnect ... ';
        $sjk->initSql('SET @@wait_timeout=3');
        $sjk->query();
        sleep(4);
        echo "go\n";

        for ($i=1;$i<=10;$i++) {
            $sjk->initSql('INSERT INTO t1 (v) values (#1)');
            $sjk->setString(1,"v'".$i);
            $this->assertEquals(true,$sjk->query());
            $this->assertEquals($i,$sjk->getInsertId());
        }
        for ($i=1;$i<=10;$i++) {
            $sjk->initSql('SELECT v FROM t1 WHERE id=#1 LIMIT 1');
            $sjk->setInt(1,$i);
            $rs=$sjk->query();
            $this->assertEquals(true,$rs instanceof SJKResult);
            $this->assertEquals(1,$rs->num());
            foreach ($rs as $row) {
                $this->assertEquals("v'".$i,$row['v']);
            }
        }

        $sjk->initSql('SELECT v FROM t1');
        $rs=$sjk->query();
        $this->assertEquals(true,$rs instanceof SJKResult);
        $this->assertEquals(10,$rs->num());
        $rows=$rs->fetchAll();
        $i=1;
        foreach ($rows as $row) {
            $this->assertEquals("v'".$i,$row['v']);
            $i++;
        }

        for ($i=1;$i<=10;$i++) {
            $sjk->initSql('DELETE FROM t1 WHERE id=#1 LIMIT 1');
            $sjk->setInt(1,$i);
            $this->assertEquals(true,$sjk->query());
            $this->assertEquals(1,$sjk->affectedRows());
        }

        $sjk->initSql('drop table if exists t1');
        $sjk->query();
        $sjk->initSql('create table t1(id int auto_increment,v1 binary(4),v_2 binary(4),PRIMARY KEY (id))TYPE=MyISAM;');
        $this->assertEquals(true,$sjk->query());
        $insert_arr=array('v1'=>'pppp','v_2'=>'iiii');
        $sjk->initSql('INSERT INTO t1 #1');
        $sjk->setInsertArray(1,$insert_arr);
        $sjk->query();
        $sjk->initSql('SELECT * FROM t1');
        $rs=$sjk->query();
        $row=$rs->fetch();
        $this->assertEquals($insert_arr['v1'],$row['v1']);
        $this->assertEquals($insert_arr['v_2'],$row['v_2']);

        $update_arr=array('v1'=>'uuuu','v_2'=>'dddd');
        $sjk->initSql('UPDATE t1 SET #1');
        $sjk->setUpdateArray(1,$update_arr);
        $sjk->query();
        $sjk->initSql('SELECT * FROM t1');
        $rs=$sjk->query();
        $row=$rs->fetch();

        $this->assertEquals($update_arr['v1'],$row['v1']);
        $this->assertEquals($update_arr['v_2'],$row['v_2']);
    }
    public function testException() {
        $sjk=$this->getSJK();
        $sjk->initSql('select * from aaa'.rand(10,10000));
        try {
            $sjk->query();
        } catch (SJKException $e) {
            $code=$e->getCode();
            $this->assertEquals($code,1146);
        }
    }
    private function getSJK() {
        return new SJK('127.0.0.1:3306','ro','urpig','sjk_test');
    }
}
